package com.zhongya.example.fragment.advanced

import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.widget.RemoteViews
import androidx.annotation.RequiresApi
import com.zhongya.example.R
import com.zhongya.example.activity.MainActivity
import com.zhongya.example.databinding.ExampleFragmentJjzg1Binding
import com.zhongya666.library.base.fragment.BaseDbFragment
import com.zhongya666.library.ext.initClose
import com.zhongya666.library.view.nav
import android.app.NotificationChannel




class JJZG1Fragment :
    BaseDbFragment<ExampleFragmentJjzg1Binding>(R.layout.example_fragment_jjzg1) {


    private lateinit var notificationManager: NotificationManager
    var id = "my_channel_01"
    var name = "my_name_01"

    @RequiresApi(Build.VERSION_CODES.O)
    override fun initView() {
        mDatabind.include.toolbar.initClose("通知") {
            nav().popBackStack()
        }

        mDatabind.run {
            tvNomal.setOnClickListener {
                sendNomalNotification()
            }
            tvFold.setOnClickListener {
                sendFoldNotification()
            }
            tvHang.setOnClickListener {
                sendHangNotification()
            }
        }

        notificationManager = context?.getSystemService(NOTIFICATION_SERVICE) as NotificationManager

    }

    override fun createObserver() {

    }

    override fun lazyLoadData() {

    }


    @RequiresApi(Build.VERSION_CODES.O)
    private fun sendNomalNotification() {
        val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
        val builder = Notification.Builder(context,id)
        val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"))
        val pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0)
        builder.setContentIntent(pendingIntent)
        builder.setSmallIcon(R.drawable.lanucher)
        builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.lanucher))
        builder.setAutoCancel(true)
        builder.setContentTitle("普通通知")
        selectNotofovatiomLevel(builder)
        notificationManager.createNotificationChannel(channel)
        notificationManager.notify(0, builder.build())
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun sendFoldNotification() {
        val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
        val builder = Notification.Builder(context,id)
        val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"))
        val pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0)
        builder.setContentIntent(pendingIntent)
        builder.setSmallIcon(R.drawable.foldleft)
        builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.lanucher))
        builder.setAutoCancel(true)
        builder.setContentTitle("折叠式通知")
        selectNotofovatiomLevel(builder)
        //用RemoteViews来创建自定义Notification视图
        val remoteViews = RemoteViews(context?.packageName, R.layout.notification_view_fold)
        val notification = builder.build()
        //指定展开时的视图
        notification.bigContentView = remoteViews
        notificationManager.createNotificationChannel(channel)
        notificationManager.notify(1, notification)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun sendHangNotification() {
        val channel = NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH)
        val builder = Notification.Builder(context,id)
        val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/"))
        val pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0)
        builder.setContentIntent(pendingIntent)
        builder.setSmallIcon(R.drawable.foldleft)
        builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.lanucher))
        builder.setAutoCancel(true)
        builder.setContentTitle("悬挂式通知")
        selectNotofovatiomLevel(builder)
        //设置点击跳转
        val hangIntent = Intent()
        hangIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        hangIntent.setClass(
            requireContext(),
            MainActivity::class.java
        )
        //如果描述的PendingIntent已经存在，则在产生新的Intent之前会先取消掉当前的
        val hangPendingIntent =
            PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT)
        builder.setFullScreenIntent(hangPendingIntent, true)
        notificationManager.createNotificationChannel(channel)
        notificationManager.notify(2, builder.build())
    }

    private fun selectNotofovatiomLevel(builder: Notification.Builder) {
        when (mDatabind.rgAll.checkedRadioButtonId) {
            R.id.rb_public -> {
                builder.setVisibility(Notification.VISIBILITY_PUBLIC)
                builder.setContentText("public")
            }
            R.id.rb_private -> {
                builder.setVisibility(Notification.VISIBILITY_PRIVATE)
                builder.setContentText("private")
            }
            R.id.rb_secret -> {
                builder.setVisibility(Notification.VISIBILITY_SECRET)
                builder.setContentText("secret")
            }
            else -> {
                builder.setVisibility(Notification.VISIBILITY_PUBLIC)
                builder.setContentText("public")
            }
        }
    }
}